<?php

namespace Chap4Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class LuckyController extends Controller
{
	/**
	 * @Route("/lucky/number")
	 * @return Response
	 */
	public function numberAction()
	{
		$number = mt_rand(0, 100);

		return new Response(
			'Lucky Number: '.$number
		);
	}

	/**
	 * JSON 返回方式1
	 * @Route("/api/lucky/number")
	 * @return Response
	 */
	public function apiNumberAction()
	{
		$data = array(
			'lucky_number' => mt_rand(0, 100)
		);

		return new Response(
			json_encode($data),
			200,
			array('Content-Type' => 'application/json')
		);
	}


	/**
	 * JSON 返回方式2
	 * @Route("/api/lucky/number2")
	 * @return Response
	 */
	public function apiNumber2Action()
	{
		$data = array(
			'lucky_number' => mt_rand(0, 100)
		);

		return new JsonResponse($data);
	}


	/**
	 * 动态URL参数
	 * @Route("/lucky/number/{count}")
	 * @param  [type] $count [description]
	 * @return [type]        [description]
	 */
	public function number2Action($count)
	{
		$numbers = array();
		for ($i=0; $i<$count; $i++) {
			$numbers[] = mt_rand(0, 100);
		}

		$numberList = implode(",", $numbers);

		return new Response(
			"Lucky Numbers: ".$numberList
		);
	}


	/**
	 * 使用templating模板服务
	 * @Route("/luckynumber/template/{count}")
	 * @param  [type] $count [description]
	 * @return [type]        [description]
	 */
	public function number3Action($count)
	{
		$numbers = array();
		for ($i=0; $i<$count; $i++) {
			$numbers[] = mt_rand(0, 100);
		}

		$numberList = implode(',', $numbers);

		/*
		$html = $this->container
			->get("templating")
			->render('Lucky/number.html.twig', array('luckyNumberList'=>$numberList));
		return new Response($html);
		*/
	
		return $this->render('Chap4Bundle:Lucky:number.html.twig', array('luckyNumberList'=>$numberList));
	}	




}
